package com.oocl.ha.merchant.controller; 
import java.util.HashMap;
import java.util.Map;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message; 
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.ws.rs.core.MediaType; 

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException; 
import com.fasterxml.jackson.databind.ObjectMapper;
import com.oocl.ha.entity.Merchant;
import com.oocl.ha.merchant.service.MerchantManager;
import com.oocl.ha.merchant.util.ConfigUtil;
import com.oocl.ha.merchant.util.UploadImgUtil;
import com.oocl.ha.merchant.vo.MerchantVO;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource; 

@Controller("mmc")
public class MerchantController {

	@Autowired
	private MerchantManager mm;
	@Autowired
	private JmsTemplate jmsTemplate;
	@Autowired
	@Qualifier("shopCheckQueue")
	private Destination merchantQuene;
	@RequestMapping("/merchant/jsp/merchantLogin")
	public ModelAndView merchantLogin(String account, String password, HttpServletRequest request) {

		Merchant merchant = mm.merchantLogin(account, password);
		if (merchant != null) {
			request.getSession().setAttribute("mId", merchant.getMid());
			request.getSession().setAttribute("account", merchant.getAccount());

			int mId = merchant.getMid();
			int statue = merchant.getStatue();
			Client client = new Client();
			client.setReadTimeout(1000);
			String webServicePath=ConfigUtil.getConfigValue("merchant.properties", "webServiceLoginPath");
			String url = webServicePath+mId;
			System.out.println(url);

			WebResource wr = client.resource(url);
			try{
			String getStatue = wr.accept(MediaType.APPLICATION_JSON_TYPE).get(String.class);
			JSONObject jsonObject = JSON.parseObject(getStatue);
			int status = jsonObject.getInteger("statue");
			System.out.println(status+"****");
				if (status == 1) {
					ModelAndView mav = new ModelAndView("/merchant/jsp/dishManager");
					return mav;
				} else {
						return new ModelAndView("/merchant/jsp/checkFail","statue",status);
				}
			
				
			}catch (Exception e) {
				return new ModelAndView("/merchant/jsp/error");
				
			}
			// return new ModelAndView("redirect:/merchant/jsp/loadAllDish");
		} else {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("isExist", "用户名密码错误");
			ModelAndView mav = new ModelAndView("/merchant/jsp/merchantLogin", map);
			return mav;
		}

	} 

	@RequestMapping("/merchant/jsp/merchantRegister")
	public String merchantRegister(String username, String password, MultipartFile icon, String shopName,
			String address) {
		Merchant merchant = new Merchant();
		merchant.setAccount(username);
		merchant.setPassword(password);
		if (icon.getSize()>0) {
			String iconName = UploadImgUtil.uploadImg(icon);
			merchant.setIcon(iconName);			
		}
		merchant.setShopName(shopName);
		merchant.setAddress(address);
		merchant.setStatue(0);
		merchant.setStar(5.0);
		merchant.setOrderNumber(0);
		mm.merchantRegister(merchant);
		final Merchant m = mm.merchantLogin(username, password);
		jmsTemplate.send(merchantQuene, new MessageCreator() {
			public Message createMessage(Session session) throws JMSException {
				ObjectMapper mapper = new ObjectMapper();

				String json = new String();
				try {
					json = mapper.writeValueAsString(m);
				} catch (JsonProcessingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				TextMessage om = session.createTextMessage(json);
				return om;
			}
		});
		return "/merchant/jsp/merchantLogin";
	}

	@RequestMapping(value = { "/merchant/jsp/merchantIsRegist" }, method = RequestMethod.GET)
	@ResponseBody
	public String merchantIsRegist(String account) {
		System.out.println(account + "________");
		boolean isRegist = mm.merchantIsRegist(account);
		if (isRegist) {
			return "y";
		} else {
			return "n";
		}
	}

	@ResponseBody
	@RequestMapping("/merchant/jsp/findMerchant")
	public ModelAndView findMerchantByMid(HttpSession session) {
		Integer mId = (Integer) session.getAttribute("mId");
		Merchant merchant = mm.findById(mId);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("m", merchant);
		ModelAndView mav = new ModelAndView("/merchant/jsp/merchentMsg", map);
		return mav;
	}

	@RequestMapping("/merchant/jsp/updateMerchantMsg")
	public String updateMerchantMsg(HttpSession session, String shopName, MultipartFile icon, String address) {
		Integer mId=(Integer) session.getAttribute("mId");
		Merchant merchant = mm.findById(mId);
		merchant.setShopName(shopName);
		merchant.setAddress(address);
		if (icon.getSize()>0) {
			String iconName = UploadImgUtil.uploadImg(icon);
			merchant.setIcon(iconName);			
		}
		mm.updateMerchantMsg(merchant);
		return "redirect:/merchant/jsp/findMerchant";
	}
	
	@RequestMapping("/merchant/jsp/logOut")
	public String merchantLogOut(HttpSession session){
		session.removeAttribute("mId");
		session.removeAttribute("account");
		return "/merchant/jsp/merchantLogin";
	}
}
